﻿using System.Windows;
using System.Windows.Input;
using PengSW.WindowHelper;

namespace ProtocolSpy
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            _Model = new MainModel
            {
                ListenPort = _Settings.ListenPort,
                TargetHost = _Settings.TargetHost,
                TargetPort = _Settings.TargetPort
            };
            this.DataContext = _Model;
        }

        private MainModel _Model;
        private Properties.Settings _Settings = Properties.Settings.Default;

        private void OnStartListen_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Exec(_Model.StartListen);
        }

        private void OnStartListen_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _Model?.CanStartListen ?? false;
        }

        private void OnStopListen_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Exec(_Model.StopListen);
        }

        private void OnStopListen_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _Model?.CanStopListen ?? false;
        }

        private void OnStartConnect_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Exec(_Model.ConnectTargetServer);
        }

        private void OnStartConnect_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _Model?.CanConnectToTargetServer ?? false;
        }

        private void OnWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _Settings.ListenPort = _Model.ListenPort;
            _Settings.TargetHost = _Model.TargetHost;
            _Settings.TargetPort = _Model.TargetPort;
            _Settings.Save();
        }
    }
}
